package com.shipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shipment.common.Result;
import com.shipment.entity.BlackUser;
import com.shipment.entity.User;
import com.shipment.mapper.BlackUserMapper;
import com.shipment.mapper.UserMapper;
import com.shipment.service.BlackUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 黑名单用户Service实现类
 */
@Service
@RequiredArgsConstructor
public class BlackUserServiceImpl extends ServiceImpl<BlackUserMapper, BlackUser> implements BlackUserService {

    private final UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addToBlacklist(Long userId) {
        // 检查用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }

        // 检查用户是否已在黑名单中
        if (isUserInBlacklist(userId)) {
            return Result.error("该用户已在黑名单中");
        }

        // 添加用户到黑名单
        BlackUser blackUser = new BlackUser();
        blackUser.setUserId(userId);
        blackUser.setCreateTime(LocalDateTime.now());
        save(blackUser);

        // 同时禁用该用户账号
        user.setStatus(0); // 设置状态为禁用
        userMapper.updateById(user);

        return Result.success("添加到黑名单成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result removeFromBlacklist(Long userId) {
        // 检查用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }

        // 检查用户是否在黑名单中
        if (!isUserInBlacklist(userId)) {
            return Result.error("该用户不在黑名单中");
        }

        // 从黑名单中移除用户
        LambdaQueryWrapper<BlackUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlackUser::getUserId, userId);
        remove(wrapper);

        // 同时启用该用户账号
        user.setStatus(1); // 设置状态为正常
        userMapper.updateById(user);

        return Result.success("从黑名单中移除成功");
    }

    @Override
    public Result getBlacklistPage(Integer current, Integer size) {
        Page<BlackUser> page = new Page<>(current, size);
        page = page(page);

        // 查询用户详细信息
        List<BlackUser> records = page.getRecords();
        List<BlackUser> blackUserList = records.stream().map(blackUser -> {
            User user = userMapper.selectById(blackUser.getUserId());
            blackUser.setUser(user);
            return blackUser;
        }).collect(Collectors.toList());

        page.setRecords(blackUserList);

        return Result.success(page);
    }

    @Override
    public boolean isUserInBlacklist(Long userId) {
        return baseMapper.checkUserInBlacklist(userId) > 0;
    }
}
